/***************************************
 * 说明：本文件主要列举第3章文中所实现的例题
 * *************************************/
#include "main.h"

/******************************************
 * 例子3.1：用构造函数对类的成员变量进行初始化
 * ****************************************/
void Time::set_time()
{
    cin >> hour;
    cin >> minute;
    cin >> second;
}
void Time::show_time()
{
    cout << hour << ":" << minute << ":" << second << endl;
}
void example1()
{
    Time time1;
    time1.set_time();
    time1.show_time();
}

/******************************************
 * 例子3.2：有两个长方体，其长宽高分别为：12.25.30；
 * 15 30 21，求他们的体积
 * ****************************************/
Box::Box(int l, int w, int h)
{
    len = l;
    width = w;
    high = h;
}
void Box::show_volume()
{
    cout << "the volume is = " << (len * width * high) << endl;
}
void example2()
{
    Box box1(12,25,30);
    box1.show_volume();
    Box box2(15, 30, 21);
    box2.show_volume();
}

/******************************************
 * 例子3.3:在例子2的基础上，定义一个无参构造函数
 * 和一个有参构造函数
 * ****************************************/
/*Box::Box()
{
    len = 10;
    width = 10;
    high = 10;
}*/
void example3()
{
    //Box box1;
    //box1.show_volume();
    Box box2(11,22,33);
    box2.show_volume();
}
/******************************************
 * 例子3.4:将例子3中的构造函数改用含默认参数的构造函数
 * ，长宽高默认为10
 * ****************************************/
void example4()
{
    Box box1;
    box1.show_volume();
    Box box2(12);
    box2.show_volume();
    Box box3(12,15);
    box3.show_volume();
}
/******************************************
 * 例子3.5：包含构造函数与析构函数的C++类
 * ****************************************/
Student::Student(int a, char s, string na)
{
    age = a;
    sex = s;
    name = na;
    cout << "this is construct" << endl;
}
Student::~Student()
{
    cout << "this is destruct" << endl;
}

void Student::show_msg()
{
    cout << "age = " << age << endl;
    cout << "sex = " << sex << endl;
    cout << "name = " << name << endl;
}
void example5()
{
    Student stu1(12, 'm', "wang");
    stu1.show_msg();
    Student stu2(11,'w', "yang");
    stu2.show_msg();
}

/******************************************
 * 例子3.6：计算和输出3个立方体的体积
 * ****************************************/
void example6()
{
    Box box[3] = {
        Box(10, 10, 10),
        Box(11, 11, 11),
        Box(12, 12, 12)
    };
    box[1].show_volume();
    box[2].show_volume();
    box[3].show_volume();
}

/******************************************
 * 例子3.7：用对象指针输出时分秒
 * ****************************************/
void example7()
{
    Time* time = new Time;
    time->set_time();
    time->show_time();
}

/******************************************
 * 例子3.8：使用对象的引用输出时分秒
 * ****************************************/
void function(Time &time)
{
    //time.set_time();
    time.show_time();
}
void example8()
{
    Time time(4,5,8);
    function(time);
    time.show_time(); // 取决于function函数的set_time函数的输入
}

/******************************************
 * 例子3.9：将一个对象赋值给另外一个对象
 * ****************************************/
Box1::Box1()
{

}
Box1::Box1(int l, int w, int h)
{
    length = l;
    width = w;
    high = h;
}
void Box1::show_volume()
{
    cout << "volume is " << length * width * high << endl;
}
void example9()
{
    Box1 box1(1,2,3);
    Box1 box2;
    box2 = box1;  // 对象box1和box2都已经存在，而后进行赋值
    box1.show_volume();
    box2.show_volume();
}

/******************************************
 * 例子3.10：使用静态数据成员，计算立方体的体积
 * ****************************************/
Box2::Box2()
{

}
Box2::Box2(int l, int w)
{
    length = l;
    width = w;
}
void Box2::show_volume()
{
    cout << "the high is " << high << endl;
    cout << "the volume is " << (length * width * high) << endl;
}
int Box2::high = 10;
void example10()
{
    Box2 box1(1,2);
    box1.show_volume();
    Box2 box2(3,4);
    box2.show_volume();
}

/******************************************
 * 例子3.11：给定若干学生的数据(包括学号、年龄、成绩)
 * 要求统计学生的平均成绩
 * ****************************************/
Student1::Student1(int n, int a, int s)
{
    number = n;
    age = a;
    score = s;
}
void Student1::comp_sum()
{
    sum = sum + score;
    count++;
}
float Student1::get_average()
{
    return (sum / count);
}
int Student1::sum = 0;
float Student1::count = 0;
void example11()
{
    Student1 stu1(1, 12, 23);
    stu1.comp_sum();
    Student1 stu2(2, 12, 34);
    stu2.comp_sum();
    Student1 stu3(3, 12, 34);
    stu3.comp_sum();
    cout << "the average score is " << Student1::get_average() << endl;
}
/******************************************
 * 例子3.12：用友元函数访问类的私有成员变量
 * ****************************************/
Time1::Time1()
{

}
Time1::Time1(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
void display(Time1& time)
{
    cout << time.hour << ":" << time.minute << ":" << time.second << endl;
}
void example12()
{
    Time1 time(12,23,45);
    display(time);
}
/**************************************************
 * 例子3.13：有一个日期（Date）类对象和一个时间（Time）
 * 类对象，均已指定了内容，要求一次输出其中的日期和时间
 * ************************************************/
Date::Date(int y, int m, int d)
{
    year = y;
    month = m;
    day = d;
}
Time2::Time2(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
void Time2::show_time(Date& date)
{
    cout << date.year << ":" << date.month << ":" << date.day << ":";
    cout << hour << ":" << minute << ":" << second << endl;
}
void example13()
{
    Time2 time(12,23,45);
    Date date(2021, 04, 16);
    time.show_time(date);
}
/**************************************************
 * 例子3.14：声明一个类模板，利用它分别实现两个整数和两个
 * 浮点数和字符的比较，求出大的数和小的数
 * ************************************************/
void example14()
{
    Compare<int>cmp1(1,2);
    cout << "cmp1 max " << cmp1.max() << endl;
    cout << "cmp1 min " << cmp1.min() << endl;

    Compare<float>cmp2(1.3, 2.1);
    cout << "cmp2 max " << cmp2.max() << endl;
    cout << "cmp2 min " << cmp2.min() << endl;

    Compare<char>cmp3('a', 'c');
    cout << "cmp3 max " << cmp3.max() << endl;
    cout << "cmp3 min " << cmp3.min() << endl;
}
int main()
{
    example14();
    return 0;
}